
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/login/controller/user_controller.dart';
import 'package:nft_shop/login/model/models.dart';
import 'package:nft_shop/net/dio_api.dart';
import 'package:nft_shop/net/service_repository.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/select_image_utils.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_app_bar.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:nft_shop/widgets/my_scroll_view.dart';
import 'package:nft_shop/widgets/my_text_field.dart';
import 'package:nft_shop/widgets/select_image.dart';

class MyAuthenticationPage extends StatefulWidget {
  const MyAuthenticationPage({Key? key}) : super(key: key);

  @override
  _MyAuthenticationPageState createState() => _MyAuthenticationPageState();
}

class _MyAuthenticationPageState extends State<MyAuthenticationPage> {
  final TextEditingController _nameController = TextEditingController();
  final TextEditingController _cardController = TextEditingController();

  final FocusNode _nodeText1 = FocusNode();
  final FocusNode _nodeText2 = FocusNode();
  String urlStr1 = "";
  String urlStr2 = "";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "实名认证",
      ),
      body: MyScrollView(
        padding: EdgeInsets.symmetric(horizontal: 16),
        children:ObjectUtil.isNotEmpty(UserController.to.member.memberGroupId)?_certifiednBody():_certifications(),
        bottomButton:Container(
          height: 40,
          margin: EdgeInsets.only(left: 14, right: 14, bottom: 27 + ScreenUtil.getBottomBarH(context)),
          decoration: BoxDecoration(
            color: Global.kTheme.colorBackground3,
            borderRadius: BorderRadius.circular(4),
          ),
          child: MyButton(
            minHeight: 40,
            onPressed: () async {
              _nodeText1.unfocus();
              _nodeText2.unfocus();
              if (_nameController.text.length == 0) {
                EasyLoading.showToast('请输入姓名');
                return;
              }
              if (_cardController.text.length == 0) {
                EasyLoading.showToast('请输入身份证号');
                return;
              }

              if(urlStr1.length == 0){
                EasyLoading.showToast('请上传身份证正面照');
                return ;
              }
              if(urlStr2.length == 0){
                EasyLoading.showToast('请上传身份证反面照');
                return ;
              }

              try {
                await ServiceRepository.authReal(urlStr1, urlStr2,_nameController.text,_cardController.text);
                Member model = UserController.to.member;
                model.realNameStatus = "1";
                UserController.to.saveUser(model);
                EasyLoading.showToast("上传成功");
                Get.back();
              } catch (e, s) {
                EasyLoading.showToast(http.setError(e,s));
              }
            },
            text: "提交",
            fontSize: 16,
            textColor: Colors.white,
            backgroundColor: Global.kTheme.colorBackground3,
          ),
        ),
      ),
    );


  }
  ///身份认证
  List<Widget> _certifications(){
    return [
      Container(
        height: 53,
        child: Align(alignment: Alignment.centerLeft, child: Text("请填写您的身份信息",style: TextStyle(fontSize: 16,color: Color(0xFF131415),fontFamily: "PingFangSC-Semibold",),)),
      ),
      SizedBox(height: 12),
      Text(
        "姓名",
        style: TextStyle(
            fontSize: 14,
            fontFamily: "PingFangSC-Semibold",
            color: Global.kTheme.textPrimary6),
      ),
      MyTextField(
        keyName: 'name',
        maxLength: 40,
        focusNode: _nodeText1,
        controller: _nameController,
        hintText:"请输入您身份证上的姓名",
      ),
      SizedBox(height: 16),
      Text(
        "身份证号",
        style: TextStyle(
            fontSize: 14,
            fontFamily: "PingFangSC-Semibold",
            color: Global.kTheme.textPrimary6),
      ),
      MyTextField(
        keyName: 'card',
        maxLength: 40,
        focusNode: _nodeText2,
        controller: _cardController,
        hintText:"请输入您的证件号",
      ),
      SizedBox(height: 14,),
      Text("请拍摄或上传身份证照片",style: TextStyle(fontSize: 16,fontFamily: Global.mySemiboldFont,color: Global.kTheme.textPrimary6),),
      SizedBox(height: 16,),
      Text("温馨提示:",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary7,),),
      Text("1、身份一旦认证成功无法更改 ",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary7,),),
      Text("2、提交后系统会审核,请耐心等待",style: TextStyle(fontSize: 12,color: Global.kTheme.textPrimary7,),),
      SizedBox(height: 30,),
      Padding(
        padding: EdgeInsets.symmetric(horizontal: 14),
        child: InkWell(
          onTap: () => urlStr1.length == 0?_showHintDialog((url){
            setState(() {
              urlStr1 = url;
            });
          }):null,
          child: Stack(
            alignment: Alignment.center,
            children: [
              ClipRRect(
                  borderRadius: BorderRadius.circular(4),child: LoadImage(urlStr1,holderImg:"my_identity1",width: double.infinity,height: 182*(Get.width-60)/315,)),
              Visibility(visible: urlStr1.length == 0?true:false, child: LoadAssetImage("my_camera",width: 68,height: 68,)),
            ],
          ),
        ),
      ),
      SizedBox(height: 24,),
      Padding(
        padding: EdgeInsets.symmetric(horizontal: 14),
        child: InkWell(
          onTap: () => urlStr2.length == 0?_showHintDialog((url){
            setState(() {
              urlStr2 = url;
            });
          }):null,
          child: Stack(
            alignment: Alignment.center,
            children: [
              ClipRRect(
                  borderRadius: BorderRadius.circular(4),child: LoadImage(urlStr2,holderImg:"my_identity2",width: double.infinity,height: 182*(Get.width-60)/315,)),
              Visibility(visible: urlStr2.length == 0?true:false,child: LoadAssetImage("my_camera",width: 68,height: 68,)),
            ],
          ),
        ),
      ),
      SizedBox(height: 20),
    ];
  }
  //已认证
  List<Widget> _certifiednBody(){
    return [
      SizedBox(height: 69),
      Center(child: LoadAssetImage("my_certified",width: 123,height: 108,)),
      SizedBox(height: 27),
      Center(child: Text("恭喜你,认证成功!",style: TextStyle(color: Global.kTheme.textPrimary1,fontSize: 16,fontFamily: Global.mySemiboldFont,),))
    ];
  }

  //弹出
  void _showHintDialog(ValueChanged<String> onTap) {
    _nodeText1.unfocus();
    _nodeText2.unfocus();
    showModalBottomSheet<void>(
      context: Get.context!,
      /// 禁止拖动关闭
      enableDrag: true,
      /// 使用true则高度不受16分之9的最高限制
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      barrierColor: Color(0x80000000),
      builder: (_) => Container(
        color: Colors.transparent,
        height: 190,
        width: 120,
        padding: EdgeInsets.symmetric(horizontal: 16),
        child: SelectedImageUtils(determineBack: (String url){
          onTap(url);
        },),
      ),
    );
  }
}
